package code.oldCode.feishuSpecializedTraining.array;

import java.util.Arrays;

/**
 * @author 26029
 * @date 2025/3/6
 * @description
 */
public class MyArray {
    // 704. 二分查找
    public int search(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while (left <= right) {
            int mid = (right - left) / 2 + left;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return -1;
    }

    // 27. 移除元素
    public int removeElement(int[] nums, int val) {
        int stayPos = 0;
        int len = nums.length;
        for (int i = 0; i < len; i++) {
            if (nums[i] != val) {
                nums[stayPos] = nums[i];
                stayPos++;
            }
        }
        return stayPos;
    }

    // 977. 有序数组的平方
    public int[] sortedSquares(int[] nums) {
        // 直接排序
        return Arrays.stream(nums).map(x -> x * x).sorted().toArray();
    }

    public int[] sortedSquares_(int[] nums) {
        // 双指针
        int len = nums.length;
        int left = 0, right = len - 1;
        int insertPos = len - 1;
        int[] ans = new int[len];
        while (left <= right) {
            int leftNum = nums[left];
            int rightNum = nums[right];
            if (Math.abs(leftNum) < Math.abs(rightNum)) {
                ans[insertPos] = rightNum * rightNum;
                right--;
            } else {
                ans[insertPos] = leftNum * leftNum;
                left++;
            }
            insertPos--;
        }
        return ans;
    }

    // 209. 长度最小的子数组
    public int minSubArrayLen(int target, int[] nums) {
        int start = 0, end = 0;
        int ans = Integer.MAX_VALUE;
        int sum = 0;
        int len = nums.length;
        while (end < len) {
            sum += nums[end];
            while (sum >= target) {
                ans = Math.min(ans, end - start + 1);
                sum -= nums[start];
                start++;
            }
            end++;
        }
        return ans == Integer.MAX_VALUE ? 0 : ans;
    }

    // 59. 螺旋矩阵 II
    public int[][] generateMatrix(int n) {
        int[][] m = new int[n][n];
        int nowNum = 1;
        int allCounts = n * n;
        int[][] direction = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
        int nowDirection = 0;
        int i = 0, j = 0;
        while (nowNum <= allCounts) {
            // 赋值
            m[i][j] = nowNum;
            nowNum++;
            // 找下一个位置
            int nextI = i + direction[nowDirection][0];
            int nextJ = j + direction[nowDirection][1];
            if (nextI >= 0 && nextI < n && nextJ >= 0 && nextJ < n && m[nextI][nextJ] == 0) {
                i = nextI;
                j = nextJ;
            } else {
                nowDirection = (nowDirection + 1) % 4;
                i = i + direction[nowDirection][0];
                j = j + direction[nowDirection][1];
            }
        }
        return m;
    }

    public static void main(String[] args) {
        MyArray myArray = new MyArray();
    }
}
